package com.ag.zhh.action;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.SessionAware;

import com.ag.bean.TbEvent;
import com.ag.bean.TbEventGrade;
import com.ag.bean.TbEventLevel;
import com.ag.bean.TbPropose;
import com.ag.bean.TbUser;
import com.ag.common.BaseAction;
import com.ag.common.EventUtil;
import com.ag.common.IPermissionService;
import com.ag.work2.service.IReportService;
import com.ag.wzp.action.EventAction;
import com.ag.wzp.service.impl.EventBaseService;
import com.ag.wzp.service.impl.EventServiceImpl;
import com.ag.zhh.service.EventUpGradeService;

/**
 * 工情分析
 * 
 * @author 张颢铧
 * 
 */
public class AnalyzeAction extends BaseAction implements SessionAware {

	private static final Logger logger = Logger.getLogger(EventAction.class);

	/**
	 * 根据EId获取Event，用于查看事件详情
	 * 
	 * @return
	 */
	public String findById() {
		logger.info("工情分析：AnalyzeAction.findById()" + findEventId());
		String eid = findEventId();
		if (eid != null && eid.length() > 0) {
			// 事件处理过程中需要session里有EId
			this.sessionMap.put(EventUtil.PROCESSING_EVENT_ID, eid);
			event = eventServiceImpl.findById(eid);
			// 获取事件等级列表
			eventLevels = eventBaseService.getAllEventLevel();
			// 获取已提交申请的事件等级变更记录
			findApplyedEventUpGradeByEId();
			// 获取当前User
			getUserBySessionMap();
			// 获取呈批表详情
			// List<TbPropose> proposesList=
			// reportService.findEventPropose(findEventId());
			// if(proposesList != null && proposesList.size() >0){
			// propose = proposesList.get(0);
			// }else {
			// propose = null;
			// }
			String eventIdString = findEventId();
			System.out.println("AnalyzeAction.findById()-------"
					+ eventIdString);
			propose = reportService.findEventPropose(eventIdString);
//			if (propose.getTbUserByPViceCommander() != null)
//				System.out.println(propose.getTbUserByPViceCommander()
//						.getUName()
//						+ "////////////**************************************");
			return "analyse";
		} else {
			return ERROR;
		}
	}

	/**
	 * 查询未审核事件等级变更表byEId
	 * 
	 * @return
	 */
	private String findApplyedEventUpGradeByEId() {
		eventGrade = this.eventUpGradeService
				.findApplyedEventUpGradeByEId(findEventId());
		if (eventGrade != null) {
			logger.info(eventGrade.getEgId());
			logger.info(eventGrade.getEgReqTime());
		}
		return "findApplyedEventUpGrade";
	}

	private void getUserBySessionMap() {
		user = pms.getCurrentUser(sessionMap);
	}

	public TbUser getUser() {
		return user;
	}

	public void setUser(TbUser user) {
		this.user = user;
	}

	private String findEventId() {
		// return "1010011001";

		return (String) this.getSessionAttribute(EventUtil.PROCESSING_EVENT_ID);
	}

	private TbEvent event;

	private TbEventGrade eventGrade;

	private List<TbEventLevel> eventLevels;

	private TbUser user;

	private TbPropose propose;

	@Resource(name = "LZMReportServiceImpl")
	private IReportService reportService;

	private Map<String, Object> sessionMap;

	@Resource(name = "eventServiceImpl")
	private EventServiceImpl eventServiceImpl;

	@Resource(name = "eventBaseService")
	private EventBaseService eventBaseService;

	@Resource(name = "PermissionServiceImpl")
	private IPermissionService pms;

	@Resource(name = "EventUpGradeService")
	private EventUpGradeService eventUpGradeService;

	public TbEvent getEvent() {
		return event;
	}

	public void setEvent(TbEvent event) {
		this.event = event;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		// TODO Auto-generated method stub
		this.sessionMap = session;
	}

	public List<TbEventLevel> getEventLevels() {
		return eventLevels;
	}

	public void setEventLevels(List<TbEventLevel> eventLevels) {
		this.eventLevels = eventLevels;
	}

	public TbEventGrade getEventGrade() {
		return eventGrade;
	}

	public void setEventGrade(TbEventGrade eventGrade) {
		this.eventGrade = eventGrade;
	}

	public TbPropose getPropose() {
		return propose;
	}

	public void setPropose(TbPropose propose) {
		this.propose = propose;
	}

}
